/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.web.controller;

import com.intelligent.ispc.common.dto.JsonResponseDto;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.web.Servlets;
import com.intelligent.ispc.core.service.GRRExportService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import java.util.Map;

/**
 * Created by cherry on 2016/6/8.
 */
@Controller
@RequestMapping(value = "/export/grr")
public class GRRExportController {

    @Autowired
    private GRRExportService grrExportService;

    @ResponseBody
    @RequestMapping(value = "result")
    public JsonResponseDto exportGRRResult(ServletRequest request, Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:grr:export")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }
        try {
            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            searchParam.put("fileType", "xls");
            grrExportService.exportResultToExcel(searchParam);
        } catch (ApplicationException e) {
            return JsonResponseDto.FAILURE().setMessage(e.getMessage());
        }

        return JsonResponseDto.SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "resultAndData")
    public JsonResponseDto exportGRRResultAndData(ServletRequest request, Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:grr:export")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }
        try {
            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            searchParam.put("fileType", "xls");
            grrExportService.exportResultAndDataToExcel(searchParam);
        } catch (ApplicationException e) {
            return JsonResponseDto.FAILURE().setMessage(e.getMessage());
        }

        return JsonResponseDto.SUCCESS();
    }
}
